package com.gopay.bis.service.bank.impl.bcdl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.mail.MessagingException;

import com.gopay.common.cipher.utils.LoggerMaskUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.SendSmsUtils;
import com.gopay.bis.manager.bank.bcdl.IcbcBcdlManager;
import com.gopay.common.constants.bank.IcbcConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BcdlMessage;
import com.gopay.common.domain.bank.BcdlResult;
import com.gopay.common.domain.bank.bcdl.BcdlPacket;
import com.gopay.common.domain.bank.bcdl.BcdlPacketRes;
import com.gopay.common.domain.bank.icbc.bcdl.pay.IcbcMessageData;
import com.gopay.common.domain.bank.icbc.bcdl.pay.IcbcPacketMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.SendMail;
import com.gopay.remote.bank.BcdlInteractRemoteService;

public class IcbcBcdlServiceImpl extends BaseBcdlServiceImpl implements BcdlInteractRemoteService {

    @Resource(name = "icbcBcdlManager")
    IcbcBcdlManager icbcBcdlManager;
    @Resource(name = "smsConfig")
    private Map<String, String> smsConfig;
    @Resource(name = "mailConfig")
    private Map<String, String> mailConfig;
    @Resource(name = "icbcConfig")
    private Map<String, String> icbcConfig;
    
    private final static Logger logger = LoggerFactory.getLogger(IcbcBcdlServiceImpl.class);

    @Override
    public BcdlResult query(BcdlMessage base) {
        BcdlResult res = new BcdlResult();
        /**央行检测 要求敏感信息掩码处理 sj 2017-11-13*/
        logger.error("query,BcdlMessage" + 
        		LoggerMaskUtils.maskString(ToStringBuilder.reflectionToString(base)));
        logger.error("query,OrderId" + base.getOrderId());
        try {
            IcbcMessageData data = (IcbcMessageData) icbcBcdlManager.query(base);
            res.setDefaultModel(data);
            logger.error("query IcbcMessageData:" + LoggerMaskUtils.maskString(ToStringBuilder.reflectionToString(data)));
            if("B0116".equals(data.getRetCode())){
            	//因付款超时，订单没有发送到银行生成交易，做失败处理
                //modified by zyt,2017-10-16,先置进行中，查询到七次，且需要做拒绝账务的时候，才置失败
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
            	res.setErrorMsg("收款行超时无响应，指令已拒绝");
            	res.setSuccess(false);
                //modified by zyt,2017-9-25，统一校验方式
            	//res.setNeedRefund(true);
                String gopayOrderId = base.getOrderId();
                res.setNeedRefund(isNeedRefund(data, base.getOrderId()));
                if(res.isNeedRefund()){
                    res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
                }
            	res.setIgnoreResult(true);
            	return res;
            }
            if (IcbcConstants.NCPAYENT_STATUS_BANK_SUCCESS.equals(data.getResult())) { // 如果银行返回状态为“7”：处理成功
                res.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);
                res.setSuccess(true);
            } /*else if (IcbcConstants.NCPAYENT_STATUS_BANK_REFUSE.equals(data.getResult())
                    || IcbcConstants.NCPAYENT_STATUS_BANK_AUTH_REFUSE.equals(data.getResult())) {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
                //delete by zyt,2017-9-25，删除此处，返回前统一设置
                //res.setNeedRefund(true);
                if(IcbcConstants.NCPAYENT_STATUS_BANK_REFUSE.equals(data.getResult())){
                	String nocheckingType = icbcConfig.get("ICBC.BCDL.SENTINEL.NOCHECKINGTYPE");
                	if(StringUtils.isNotBlank(data.getInstrRetCode())
                			&& StringUtils.isNotBlank(data.getInstrRetMsg())
                			&& nocheckingType.indexOf(data.getInstrRetCode())>-1
                			&& !("RJ90".equals(data.getInstrRetCode())&&data.getInstrRetMsg().indexOf("其他差错")>-1)){
                		res.setIgnoreResult(true);
                	}
                
                }
                if(StringUtils.isNotBlank(data.getInstrRetCode())){
                	res.setBankRetCode(data.getInstrRetCode());
                }else{
                	res.setBankRetCode(data.getIRetCode());
                }
                if(StringUtils.isNotBlank(data.getInstrRetMsg())){
                	res.setErrorMsg(data.getInstrRetMsg());
                }else{
                	res.setErrorMsg(data.getIRetMsg());
                }
                
                res.setSuccess(false);
            }*/else if(IcbcConstants.NCPAYENT_STATUS_WAIT_BANK_REPLY.equals(data.getResult())){
            	res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
            	//add by lilili 大额返回“D0504”不做熔断处理
            	if(StringUtils.isNotBlank(data.getIRetCode()) && "D0504".equals(data.getIRetCode())){
            		res.setIgnoreResult(true);
            	}
            	if(StringUtils.isNotBlank(data.getInstrRetCode()) && "D0504".equals(data.getInstrRetCode())){
            		res.setIgnoreResult(true);
            	}
            	 if(StringUtils.isNotBlank(data.getInstrRetCode())){
                 	res.setBankRetCode(data.getInstrRetCode());
                 }else{
                 	res.setBankRetCode(data.getIRetCode());
                 }
            	 
            	 if(StringUtils.isNotBlank(data.getInstrRetMsg())){
                 	res.setErrorMsg(data.getInstrRetMsg());
                 }else{
                 	res.setErrorMsg(data.getIRetMsg());
                 }
                 res.setSuccess(false);
            }else if(IcbcConstants.NCPAYENT_STATUS_SUBMIT_SUCC.equals(data.getResult()) || IcbcConstants.NCPAYENT_STATUS_BANK_EXECUTE.equals(data.getResult())){
				res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
				// add by lilili 返回“0” 银行正在处理 不做熔断处理
				if (StringUtils.isNotBlank(data.getIRetCode()) && IcbcConstants.NCPAYENT_STATUS_SUBMIT_SUCC.equals(data.getIRetCode())) {
					res.setIgnoreResult(true);
				}
				if (StringUtils.isNotBlank(data.getInstrRetCode())) {
					res.setBankRetCode(data.getInstrRetCode());
				} else {
					res.setBankRetCode(data.getIRetCode());
				}

				if (StringUtils.isNotBlank(data.getInstrRetMsg())) {
					res.setErrorMsg(data.getInstrRetMsg());
				} else {
					res.setErrorMsg(data.getIRetMsg());
				}
				res.setSuccess(false);
            }else {
                //modified by zyt,2018-1-25，“是否做拒绝账务”为唯一判断失败的标准，其余非成功的均置进行中
                //start++++++++++++++++
                boolean isNeedRefund = isNeedRefund(data, base.getOrderId());
                res.setNeedRefund(isNeedRefund);
                if(isNeedRefund){
                    res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
                    if(IcbcConstants.NCPAYENT_STATUS_BANK_REFUSE.equals(data.getResult())){
                        String nocheckingType = icbcConfig.get("ICBC.BCDL.SENTINEL.NOCHECKINGTYPE");
                        if(StringUtils.isNotBlank(data.getInstrRetCode())
                                && StringUtils.isNotBlank(data.getInstrRetMsg())
                                && nocheckingType.indexOf(data.getInstrRetCode())>-1
                                && !("RJ90".equals(data.getInstrRetCode())&&data.getInstrRetMsg().indexOf("其他差错")>-1)){
                            res.setIgnoreResult(true);
                        }

                    }
                    if(StringUtils.isNotBlank(data.getInstrRetCode())){
                        res.setBankRetCode(data.getInstrRetCode());
                    }else{
                        res.setBankRetCode(data.getIRetCode());
                    }
                    if(StringUtils.isNotBlank(data.getInstrRetMsg())){
                        res.setErrorMsg(data.getInstrRetMsg());
                    }else{
                        res.setErrorMsg(data.getIRetMsg());
                    }

                    res.setSuccess(false);
                } else {
                    res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
                    res.setSuccess(false);
                }
                //end++++++++++++++++
            }
            return res;
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(e.getErrCode());
            res.setSuccess(false);
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
            res.setSuccess(false);
        }
        return res;
    }

    @Override
    public BcdlResult eft(BcdlMessage base) {
        BcdlResult res = new BcdlResult();
        logger.error("eft,BcdlMessage" + LoggerMaskUtils.maskString(ToStringBuilder.reflectionToString(base)));
        try {
            IcbcMessageData data = (IcbcMessageData) icbcBcdlManager.eft(base);
            res.setDefaultModel(data);
            logger.error("eft IcbcMessageData:" + LoggerMaskUtils.maskString(ToStringBuilder.reflectionToString(data)));
            if (IcbcConstants.NCPAYENT_STATUS_BANK_SUCCESS.equals(data.getResult())) { // 如果银行返回状态为“7”：处理成功
                res.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);
                res.setSuccess(true);
            } else {
                //modified by zyt,2018-1-25，“是否做拒绝账务”为唯一判断失败的标准，其余非成功的均置进行中
                //start++++++++++++++++
                boolean isNeedRefund = isNeedRefund(data, base.getOrderId());
                res.setNeedRefund(isNeedRefund);
                if(isNeedRefund) {
                    res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
                    res.setBankRetCode(data.getIRetCode());
                    res.setErrorMsg(data.getIRetMsg());
                    res.setSuccess(false);
                } else {
                    res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
                    res.setSuccess(false);
                }
                //end++++++++++++++++
            }
            /*else if (IcbcConstants.NCPAYENT_STATUS_BANK_REFUSE.equals(data.getResult())
                    || IcbcConstants.NCPAYENT_STATUS_BANK_AUTH_REFUSE.equals(data.getResult())) {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
                res.setBankRetCode(data.getIRetCode());
                res.setErrorMsg(data.getIRetMsg());
                res.setSuccess(false);
                //modified by zyt,2017-10-16，设置是否需要做拒绝账务
                res.setNeedRefund(isNeedRefund(data , base.getOrderId()));
            } else {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
                res.setSuccess(false);
            }*/
            return res;
        } catch (GopayException e) {
            if (ProcCodeConstants.PROC_CODE_100E6047.equals(e.getErrCode())
                    || ProcCodeConstants.PROC_CODE_100E6082.equals(e.getErrCode())) {
            	String custName = getCustName(base.getOrderId());
                SendSmsUtils.sendSms(base.getGopayBankOrderNo(), smsConfig.get("switch"), "ICBC",
                        smsConfig.get("mobile"), e.getErrMsg()+"，商户名称："+custName+",金额："+base.getAmount().toString());
            }
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(e.getErrCode());
            res.setSuccess(false);
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
            res.setSuccess(false);
        }
        return res;
    }

    @Override
    public BcdlResult sendBank(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlResult queryBalance(BcdlMessage base) {
        BcdlResult res = new BcdlResult();
        try {
            IcbcMessageData data = (IcbcMessageData) icbcBcdlManager.queryBal(base);
            if (IcbcConstants.QUERYBAL_STATUS_SUBMIT_SUCC.equals(data.getResult())) { // 如果银行返回状态为“7”：处理成功
                res.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);
                res.setSuccess(true);
                res.setModel("payAccNum", data.getPayAccNo());
                res.setModel("balance", data.getBalance());
            } else if (IcbcConstants.NCPAYENT_STATUS_BANK_REFUSE.equals(data.getResult())
                    || IcbcConstants.NCPAYENT_STATUS_BANK_AUTH_REFUSE.equals(data.getResult())) {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
                res.setBankRetCode(data.getIRetCode());
                res.setErrorMsg(data.getIRetMsg());
                res.setSuccess(false);
            } else {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
                res.setSuccess(false);
            }
            return res;
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(e.getErrCode());
            res.setSuccess(false);
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
            res.setSuccess(false);
        }
        return res;
    }

    @Override
    public BcdlResult sendBankPacket(BcdlPacket bcdlPacket) {
        BcdlResult res = new BcdlResult();
        logger.error("sendBankPacket,BcdlPacket" + ToStringBuilder.reflectionToString(bcdlPacket));
        res.setPacketNo(bcdlPacket.getPacketNo()); // 批量包号
        try {
            IcbcMessageData data = (IcbcMessageData) icbcBcdlManager.eft(bcdlPacket);
            res.setDefaultModel(data);
            logger.error("sendBankPacket IcbcMessageData:" + 
            		LoggerMaskUtils.maskString(ToStringBuilder.reflectionToString(data)));
            if (IcbcConstants.NCPAYENT_STATUS_SUBMIT_SUCC.equals(data.getRetCode())) { 
                // 银行返回状态为“0”：批量指令提交成功
                res.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);
                res.setSuccess(true);
            } else if (IcbcConstants.NCPAYENT_STATUS_BANK_REFUSE.equals(data.getRetCode())
                    || IcbcConstants.NCPAYENT_STATUS_BANK_AUTH_REFUSE.equals(data.getRetCode())) {
                // 银行返回状态为“6”或“8”：批量指令提交失败
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
                res.setBankRetCode(data.getRetCode());
                res.setErrorMsg(data.getRetMsg());
                res.setSuccess(false);
            } else {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
                res.setErrorMsg(data.getRetMsg());
                res.setSuccess(false);
            }
            return res;
        } catch (GopayException e) {
            if (ProcCodeConstants.PROC_CODE_100E6047.equals(e.getErrCode())
                    || ProcCodeConstants.PROC_CODE_100E6082.equals(e.getErrCode())) {
                SendSmsUtils.sendSms(bcdlPacket.getPacketNo(), smsConfig.get("switch"), "ICBC",
                        smsConfig.get("mobile"), e.getErrMsg());
                if ("on".equals(mailConfig.get("mailSwitch"))) {
                    sendMail(bcdlPacket);
                }
            }
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(e.getErrCode());
            res.setSuccess(false);
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
            res.setSuccess(false);
        }
        return res;
    }

    @Override
    public BcdlPacketRes queryPacket(BcdlPacket bcdlPacket) {
        BcdlPacketRes result = new BcdlPacketRes();
        logger.error("queryPacket,BcdlPacket" + ToStringBuilder.reflectionToString(bcdlPacket));
        result.setPacketNo(bcdlPacket.getPacketNo()); // 批量包号
        try {
            IcbcPacketMessage datas = (IcbcPacketMessage) icbcBcdlManager.query(bcdlPacket);

            logger.error("queryPacket IcbcPacketMessage:" + 
            		LoggerMaskUtils.maskString(ToStringBuilder.reflectionToString(datas)));
            if("B0116".equals(datas.getRetCode())){
            	//因付款超时，订单没有发送到银行生成交易，做失败处理
            	BcdlResult res = new BcdlResult();
            	List<BcdlResult> resultList = new ArrayList<BcdlResult>(); // 批量查询接口返回list
            	res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
            	res.setErrorMsg("收款行超时无响应，指令已拒绝");
            	res.setSuccess(false);
            	res.setIgnoreResult(true);
                res.setNeedRefund(true);
            	resultList.add(res);
            	result.setBcdlResults(resultList);
            	result.setSuccess(true);
                result.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);
                result.setResultMsg("收款行超时无响应，指令已拒绝");
                return result;
            }

            List<IcbcMessageData> resDatas = datas.getIcbcMessageDatas();
            if (null != resDatas && resDatas.size() > 0) {
                List<BcdlResult> resultList = new ArrayList<BcdlResult>(); // 批量查询接口返回list
                for (int i = 0; i < resDatas.size(); i++) {
                    // 循环处理批量包查询数据
                    IcbcMessageData data = resDatas.get(i);
                    logger.error("IcbcMessageData:" + i + "========" + 
                    		LoggerMaskUtils.maskString(ToStringBuilder.reflectionToString(datas)));
                    BcdlResult res = new BcdlResult();
                    res.setDefaultModel(data); // 查询返回对象
                    res.setGopayOrderId(data.getQryiSeqno()); // 订单号
                    if (IcbcConstants.NCPAYENT_STATUS_BANK_SUCCESS.equals(data.getResult())) { // 如果银行返回状态为“7”：处理成功
                        res.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);
                        res.setSuccess(true);
                    } /*else if (IcbcConstants.NCPAYENT_STATUS_BANK_REFUSE.equals(data.getResult())
                            || IcbcConstants.NCPAYENT_STATUS_BANK_AUTH_REFUSE.equals(data.getResult())) {
                        res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
                        //delete by zyt，2017-9-25，注释此处，添加到list前统一设置
                        //res.setNeedRefund(true);
                        if(IcbcConstants.NCPAYENT_STATUS_BANK_REFUSE.equals(data.getResult())){
                        	String nocheckingType = icbcConfig.get("ICBC.BCDL.SENTINEL.NOCHECKINGTYPE");
                        	if(StringUtils.isNotBlank(data.getInstrRetCode())
                        			&& StringUtils.isNotBlank(data.getInstrRetMsg())
                        			&& nocheckingType.indexOf(data.getInstrRetCode())>-1
                        			&& !("RJ90".equals(data.getInstrRetCode())&&data.getInstrRetMsg().indexOf("其他差错")>-1)){
                        		res.setIgnoreResult(true);
                        	}
                        }
                        if(StringUtils.isNotBlank(data.getInstrRetCode())){
    	                 	res.setBankRetCode(data.getInstrRetCode());
    	                 }else{
    	                 	res.setBankRetCode(data.getIRetCode());
    	                 }
                        
                        if(StringUtils.isNotBlank(data.getInstrRetCode())){
                        	res.setBankRetCode(data.getInstrRetCode());
                        }else{
                        	res.setBankRetCode(data.getIRetCode());
                        }
                        
                        if (StringUtils.isNotBlank(data.getInstrRetMsg())) {
                            res.setErrorMsg(data.getInstrRetMsg());
                        } else {
                            res.setErrorMsg(data.getIRetMsg());
                        }
                        res.setSuccess(false);
					} */else if (IcbcConstants.NCPAYENT_STATUS_WAIT_BANK_REPLY.equals(data.getResult())) {
						res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
						// add by lilili 返回“D0504”不做熔断处理
						if (StringUtils.isNotBlank(data.getIRetCode()) && "D0504".equals(data.getIRetCode())) {
							res.setIgnoreResult(true);
						}
						if (StringUtils.isNotBlank(data.getInstrRetCode()) && "D0504".equals(data.getInstrRetCode())) {
							res.setIgnoreResult(true);
						}

						
						if (StringUtils.isNotBlank(data.getInstrRetCode())) {
							res.setBankRetCode(data.getInstrRetCode());
						} else {
							res.setBankRetCode(data.getIRetCode());
						}

						if (StringUtils.isNotBlank(data.getInstrRetMsg())) {
							res.setErrorMsg(data.getInstrRetMsg());
						} else {
							res.setErrorMsg(data.getIRetMsg());
						}
						res.setSuccess(false);
					} else if (IcbcConstants.NCPAYENT_STATUS_SUBMIT_SUCC.equals(data.getResult()) || IcbcConstants.NCPAYENT_STATUS_BANK_EXECUTE.equals(data.getResult())) {
						res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
						// add by lilili 返回“0” 银行正在处理 不做熔断处理
						if (StringUtils.isNotBlank(data.getIRetCode()) && IcbcConstants.NCPAYENT_STATUS_SUBMIT_SUCC.equals(data.getIRetCode())) {
							res.setIgnoreResult(true);
						}

						if (StringUtils.isNotBlank(data.getInstrRetCode())) {
							res.setBankRetCode(data.getInstrRetCode());
						} else {
							res.setBankRetCode(data.getIRetCode());
						}

        				if(StringUtils.isNotBlank(data.getInstrRetCode())){
    	                 	res.setBankRetCode(data.getInstrRetCode());
    	                 }else{
    	                 	res.setBankRetCode(data.getIRetCode());
    	                 }
						if (StringUtils.isNotBlank(data.getInstrRetMsg())) {
							res.setErrorMsg(data.getInstrRetMsg());
						} else {
							res.setErrorMsg(data.getIRetMsg());
						}
        				res.setSuccess(false);
                    }else {
                        //modified by zyt,2018-1-25，“是否做拒绝账务”为唯一判断失败的标准，其余非成功的均置进行中
                        //start+++++++++++++++++++++
                        //add by zyt,2017-10-19，批量查询的情况下，子级数据中，retCode，transCode均为空，赋值进去，便于内部校验
                        data.setRetCode(datas.getRetCode());
                        data.setTransCode(datas.getTransCode());
                        boolean isNeedRefund = isNeedRefund(data, data.getPostScript());
                        res.setNeedRefund(isNeedRefund);
                        if(isNeedRefund){
                            res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
                            if(IcbcConstants.NCPAYENT_STATUS_BANK_REFUSE.equals(data.getResult())){
                                String nocheckingType = icbcConfig.get("ICBC.BCDL.SENTINEL.NOCHECKINGTYPE");
                                if(StringUtils.isNotBlank(data.getInstrRetCode())
                                        && StringUtils.isNotBlank(data.getInstrRetMsg())
                                        && nocheckingType.indexOf(data.getInstrRetCode())>-1
                                        && !("RJ90".equals(data.getInstrRetCode())&&data.getInstrRetMsg().indexOf("其他差错")>-1)){
                                    res.setIgnoreResult(true);
                                }
                            }
                            if(StringUtils.isNotBlank(data.getInstrRetCode())){
                                res.setBankRetCode(data.getInstrRetCode());
                            }else{
                                res.setBankRetCode(data.getIRetCode());
                            }

                            if (StringUtils.isNotBlank(data.getInstrRetMsg())) {
                                res.setErrorMsg(data.getInstrRetMsg());
                            } else {
                                res.setErrorMsg(data.getIRetMsg());
                            }
                            res.setSuccess(false);
                        } else {
                            res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
                            res.setSuccess(false);
                        }
                        //end+++++++++++++++++++++
                    }
                    //add by zyt,2017-9-25，根据返回报文，设置是否失败，且需要做拒绝账务
                    if(res.isSuccess() == false){
                        //add by zyt,2017-10-19，批量查询的情况下，子级数据中，retCode，transCode均为空，赋值进去，便于内部校验
                        data.setRetCode(datas.getRetCode());
                        data.setTransCode(datas.getTransCode());
                        //PostScript存储的是订单号
                        res.setNeedRefund(isNeedRefund(data, data.getPostScript()));
                    }
                    resultList.add(res);
                }
                result.setBcdlResults(resultList);
            }
            
            result.setSuccess(true);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);
            result.setResultMsg(datas.getRetMsg());
            return result;
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
            result.setSuccess(false);
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
            result.setSuccess(false);
        }
        return result;
    }

    @Override
    public BcdlResult sendBankQueryBalance(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlResult eftRetry(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }
    
    private void sendMail(BcdlPacket bcdlPacket) {
        try {
            // 批量付款请求超时发送邮件明细
            String subject = "工行批量付款超时订单";
            String[] cc = mailConfig.get("sendmail.cc").split(","); // 获取多个抄送人
            List<String> ccList = Arrays.asList(cc);
            List<String> sendFile = new ArrayList<String>(); // 附件
            List<String> names = new ArrayList<String>();
            StringBuffer sbu = new StringBuffer("");
            sbu.append("工行批量付款包号: " + bcdlPacket.getPacketNo() + ", 订单明细如下: " + "\n");

            for (int i = 0; i < bcdlPacket.getBcdlMessages().size(); i++) {
                BcdlMessage bcdlMessage = bcdlPacket.getBcdlMessages().get(i);
                String custName = getCustName(bcdlMessage.getOrderId());
                sbu.append("订单号: " + bcdlMessage.getOrderId() + ", 商户名称：" + custName + ", 金额："
                        + bcdlMessage.getAmount().toString() + "\n");
            }

            SendMail.sendMessage(mailConfig.get("sendmail.host"), mailConfig.get("sendmail.sender"),
                    mailConfig.get("sendmail.username"), mailConfig.get("sendmail.password"),
                    mailConfig.get("sendmail.receiver"), subject, sbu.toString(), "text/html;charset=gb2312", ccList,
                    sendFile, names);
        } catch (MessagingException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
        }
    }

    @Override
    public BcdlResult queryAcctDetail(BcdlMessage base) {
        BcdlResult res = new BcdlResult();
        try {
            IcbcMessageData data = (IcbcMessageData) icbcBcdlManager.qryAcctDetail(base);
            if (IcbcConstants.QUERYBAL_STATUS_SUBMIT_SUCC.equals(data.getRetCode())) {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);
                res.setSuccess(true);
                res.setModel("acctDetail", data);
            } else {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
                res.setErrorMsg(data.getErrMsg());
                res.setSuccess(false);
            }
            return res;
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(e.getErrCode());
            res.setSuccess(false);
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
            res.setSuccess(false);
        }
        return res;
    }

}
